package com.powernode.controller;

import com.powernode.domain.Category;
import com.powernode.model.Result;
import com.powernode.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RenBoQing
 * @date 2024年07月27日 11:45
 * @Description 商品分类控制器
 */
@Api(tags = "商品分类接口管理")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /*
     * 查询商品所有类目
     * @author RenBoQing
     * @date 2024/7/27 11:46
     * @return com.powernode.model.Result<java.util.List<com.powernode.domain.Category>>
     */
    @ApiOperation("查询商品所有类目")
    @GetMapping("/table")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<List<Category>> loadCategoryList() {
        List<Category> categories = categoryService.queryAllCategoryList();
        return Result.success(categories);
    }
   /*
    * 查询商品一级类目
    * @author RenBoQing
    * @date 2024/8/25 12:11
    * @return com.powernode.model.Result<java.util.List<com.powernode.domain.Category>>
    */
    @ApiOperation("查询商品一级类目")
    @GetMapping("/listCategory")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<List<Category>> loadFirstCategoryList() {
        List<Category> categories = categoryService.queryFirstCategoryList();
        return Result.success(categories);
    }
    /*
     * 新增商品类目
     * @author RenBoQing
     * @date 2024/7/27 14:26
     * @param category
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("新增商品类目")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('prod:category:save')")
    public Result<String> saveCategory(@RequestBody Category category) {
        boolean success = categoryService.saveCategory(category);
        return Result.handle(success);
    }

    /*
     * 根据类目的id查询商品类目的详情
     * @author RenBoQing
     * @date 2024/7/27 14:50
     * @param categoryId
     * @return com.powernode.model.Result<com.powernode.domain.Category>
     */
    @ApiOperation("根据类目的id查询商品类目的标识")
    @GetMapping("/getCategoryById/{id}")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<Category> getCategoryById(@PathVariable("id") Long categoryId) {
        Category category = categoryService.getById(categoryId);
        return Result.success(category);
    }
    /*
     * 修改商品类目
     * @author RenBoQing
     * @date 2024/7/27 14:50
     * @param category
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("修改商品类目")
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('prod:category:update')")
    public Result<String> updateCategory(@RequestBody Category category) {
        boolean success = categoryService.updateCategory(category);
        return Result.handle(success);
    }

    /*
     * 删除商品类目
     * @author RenBoQing
     * @date 2024/7/27 15:54
     * @param categoryId
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("删除商品类目")
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('prod:category:delete')")
    public Result<String> deleteCategory(@PathVariable("id") Long categoryId) {
        boolean success = categoryService.removeCategoryById(categoryId);
        return Result.handle(success);
    }
}
